package netty.v3.handler;

import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import netty.v3.req.LoginReq;
import netty.v3.serializer.PacketCodeC;
import org.apache.commons.lang3.StringUtils;

public class ServerLoginHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        PacketCodeC packetCodeC = new PacketCodeC();
        Object packet = packetCodeC.decode(byteBuf);
        if(packet instanceof LoginReq){
            LoginReq loginReqPacket = (LoginReq) packet;
            if(validateLogin(loginReqPacket)){

            }
        }
        System.out.println(JSONObject.toJSONString(packet));
    }

    public boolean validateLogin(LoginReq loginReqPacket){
        if(loginReqPacket == null || !StringUtils.equals(loginReqPacket.getPassword(), "123456")){
            return false;
        }
        return true;
    }
}
